<?php
/**
 * SOAP component
 *
 * @author      RosSoft
 * @version     0.1
 * @license		 MIT
 * @package components
 */

class SoapComponent extends Object
{
	var $controller;
	var $soapclient;

	//var $components=array('cacheObject');

	/**
	 * Enables the use of cache for the server WSDL file
	 */
	var $wsdl_cache=false;

	function startup(&$controller)
	{
		$this->controller=& $controller;

	}

	/**
	 * Execute directly a remote method from an URL
	 * @param string $url The url
	 * @param string $func The name of the function
	 * @param array $param An associative array of parameters
	 */

	function client($url,$func,$param=array())
	{
		App::import('Vendor','nusoap/nusoap');
		Configure::write('debug',0);
		
		$wsdl=$this->_get_wsdl($url);

		//you have to rename all the instances of soapclient to soap_client in the file nusoap.php (PHP5 compat)
		$this->soapclient = new soap_client($wsdl,true);
		$response = $this->soapclient->call($func, $param);
		return $response;
	}

	/**
	 * Returns a proxy object for calling remote methods from an URL
	 * @param string $url The url
	 * @return object The proxy object
	 */

	function & client_proxy($url)
	{
		App::import('Vendor','nusoap/nusoap');

		$wsdl=$this->_get_wsdl($url);
		$this->soapclient = new soap_client($wsdl,true);

		$proxy = $this->soapclient->getProxy();
		return $proxy;
	}


	/**
	 * Retrieves a wsdl object with the WSDL file parsed. It caches the result
	 * @param string $url Url of the WSDL file
	 * @return object Instance of wsdl class
	 */
	function & _get_wsdl($url)
	{
		if ($this->wsdl_cache)
		{
			$cacheName='soapclient/' . md5($url) . '.wsdl';

			$wsdl=$this->cacheObject->read($cacheName);
			if (! $wsdl)
			{
				$wsdl =& new wsdl($url,false,false,false,false,0,30);
				$this->cacheObject->write($cacheName,$wsdl,'+1 Day',2);
			}
		}
		else
		{
			$wsdl =& new wsdl($url,false,false,false,false,0,30);
		}
		return $wsdl;
	}


	/**
	 * Returns the last response from server
	 * @return array('http'=>httpdata, 'data'=>soap payload of http data)
	 */
	function response()
	{
		return array(	'http'=>$this->soapclient->response,
						'data'=>$this->soapclient->responseData);
	}

	/*
	//obsolete
	function service($name=false)
	{
		global $api;
		global $controller;

		//clean the output
		ob_end_clean();
		$this->controller->autoRender=false;

		//include the service
		$controller=$this->controller;
		$name=$this->controller->action;
		$servicefile=Inflector::underscore($this->controller->name) .DS . $name . '.php';
		require_once CONTROLLERS . 'soap_services' . DS . $servicefile;

		//create a soap server
		vendor('webservices/nusoap');
		$server=new soap_server();
		//$server->wsdl->addComplexType(<br />

		$wsdl="{$name}wsdl";
		$urn="urn:$wsdl";
		$server->configureWSDL($wsdl, $urn);
		//Register the method to expose
		$server->register(	$name,$api['input'],$api['output'],
						  	$urn, "$urn#$name",
					    	'rpc',		// style
    						'encoded', // use
    						$api['doc']);	// documentation

		global $HTTP_RAW_POST_DATA;
		$data = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
		$server->service($data);
	}
	*/
}